﻿Ext.define("PSI.InConsignation.FeeMainForm", {
    createUI: function () {
        var me = this;

        var wnd = Ext.create("Ext.window.Window", {
            title: "费用",
            modal: true,
            onEsc: Ext.emptyFn,
            width: 1000,
            height: 600,
            layout: "border",
            items: [
                {
                    region: "north",
                    height: 200,
                    title: "箱",
                    layout: "fit",
                    items: [
                        me.getCntGrid()
                    ],
                    split: true
                }, {
                    region: "center",
                    xtype: "tabpanel",
                    items: [
                        {
                            title: "箱费用明细",
                            layout: "fit",
                            items: [
                                me.getFeeGrid()
                            ]
                        }, {
                            title: "委托费用汇总",
                            layout: "fit",
                            items: [
                                me.getFeeSumGrid()
                            ]
                        }
                    ]
                }
            ]
        });

        me.wnd = wnd;
    },

    show: function (parentForm, consignationId) {
        var me = this;

        me.parentForm = parentForm;
        me.consignationId = consignationId;

        me.createUI();

        me.wnd.on("show", function () { me.onFormShow(); }, me);

        me.wnd.show();
    },

    onFormShow: function () {
        this.refreshCntGrid();
    },

    getCntGrid: function () {
        var me = this;
        if (me.__cntGrid == null) {
            me.__cntGrid = me.createCntGrid();
        }

        return me.__cntGrid;
    },

    createCntGrid: function () {
        var me = this;
        var modelName = "PSIFeeCntModel";
        Ext.define(modelName, {
            extend: "Ext.data.Model",
            fields: ["id", "blNumber", "cntOwner", "cntType", "cntNumber", "address", "goodsOwner",
                "goodsOwnerTel", "unloadMan", "unloadManTel",
                 "memo", "goods", "vsl", "clients"
            ]
        });

        return Ext.create("Ext.grid.Panel", {
            viewConfig: { enableTextSelection: true },
            columnLines: true,
            columns: [
                    Ext.create("Ext.grid.RowNumberer", { text: "", width: 30 }),
                    { header: "船名/航次", dataIndex: "vsl", menuDisabled: true },
                    { header: "委托方", dataIndex: "clients", menuDisabled: true },
                    { header: "箱号/封号", dataIndex: "cntNumber", menuDisabled: true, width: 250 },
                    { header: "箱型", dataIndex: "cntType", menuDisabled: true },
                    { header: "箱属", dataIndex: "cntOwner", menuDisabled: true },
                    { header: "门点", dataIndex: "address", menuDisabled: true },
                    { header: "货名", dataIndex: "goods", menuDisabled: true },
                    { header: "货主", dataIndex: "goodsOwner", menuDisabled: true },
                    { header: "货主电话", dataIndex: "goodsOwnerTel", menuDisabled: true },
                    { header: "卸货联系人", dataIndex: "unloadMan", menuDisabled: true },
                    { header: "卸货联系人电话", dataIndex: "unloadManTel", menuDisabled: true },
                    { header: "运单号", dataIndex: "blNumber", menuDisabled: true }
            ],
            store: Ext.create("Ext.data.Store", {
                autoLoad: false,
                model: modelName,
                data: []
            })
        });
    },

    getFeeGrid: function () {
        var me = this;
        if (!me.__feeGrid) {
            me.__feeGrid = me.createFeeGrid();
        }

        return me.__feeGrid;
    },

    createFeeGrid: function () {
        var me = this;
        var modelName = "PSIFeeModel";
        Ext.define(modelName, {
            extend: "Ext.data.Model",
            fields: ["id", "clients", "feeName", "feeMoney", "dir", "createDT"
            ]
        });

        return Ext.create("Ext.grid.Panel", {
            viewConfig: { enableTextSelection: true },
            columnLines: true,
            columns: [
                    Ext.create("Ext.grid.RowNumberer", { text: "", width: 30 }),
                    { header: "应收/应付", dataIndex: "dir", menuDisabled: true },
                    { header: "支付方", dataIndex: "clients", menuDisabled: true },
                    { header: "费目", dataIndex: "feeName", menuDisabled: true },
                    { header: "金额", dataIndex: "feeMoney", menuDisabled: true },
                    { header: "录入时间", dataIndex: "createDT", menuDisabled: true }
            ],
            store: Ext.create("Ext.data.Store", {
                autoLoad: false,
                model: modelName,
                data: []
            })
        });
    },

    getFeeSumGrid: function () {
        var me = this;
        if (!me.__feeSumGrid) {
            me.__feeSumGrid = me.createFeeSumGrid();
        }

        return me.__feeSumGrid;
    },

    createFeeSumGrid: function () {
        var me = this;
        var modelName = "PSIFeeSumModel";
        Ext.define(modelName, {
            extend: "Ext.data.Model",
            fields: ["id", "clients", "feeName", "feeMoney", "dir"
            ]
        });

        return Ext.create("Ext.grid.Panel", {
            viewConfig: { enableTextSelection: true },
            columnLines: true,
            columns: [
                    Ext.create("Ext.grid.RowNumberer", { text: "", width: 30 }),
                    { header: "应收/应付", dataIndex: "dir", menuDisabled: true },
                    { header: "支付方", dataIndex: "clients", menuDisabled: true },
                    { header: "费目", dataIndex: "feeName", menuDisabled: true },
                    { header: "金额", dataIndex: "feeMoney", menuDisabled: true }
            ],
            store: Ext.create("Ext.data.Store", {
                autoLoad: false,
                model: modelName,
                data: []
            })
        });
    },

    refreshCntGrid: function () {
        var me = this;

        var grid = me.getCntGrid();
        var store = grid.getStore();
        var el = grid.getEl() || Ext.getBody();

        el.mask("数据加载中...");
        Ext.Ajax.request({
            url: "/Fee/GetInConsignationFeeCnt",
            params: { id: me.consignationId },
            method: "POST",
            callback: function (options, success, response) {
                store.removeAll();

                if (success) {
                    var data = Ext.JSON.decode(response.responseText);
                    store.add(data);
                } else {
                    PSI.MsgBox.showInfo("网络错误");
                }

                el.unmask();
            }
        });
    }
});